<% searchMode = false %>

<% content_for :title do %>
  <% if params[:browse] %>
    <%= t("site.current.browse") %>
  <% elsif params[:catalogue] %>
    <%=t('catalogue.title') %>
  <% elsif params[:directory] %>
    <%=t('catalogue.title_directory') %>
  <% elsif params[:archive] %>
    <%=t('catalogue.title_archive') %>
  <% else %>
    <% searchMode = true %>
    <%= t("site.current.search") %>
  <% end %>
<% end %>
<%content_for :keywords do%><%= t('site.current.keywords') %><%end%>
<%content_for :description do%><%= t('site.meta_desc.search')%><%end%>

<%content_for :alternate_urls do%><%=raw alternate_urls(request.original_url, nil)%><%end%>

<% searchTerms = !params[:q].blank? and searchMode %>

<section id="search" class="">
  <div class="row">
    <div id="search-sidebar" class="search-filterbar">

      <div class="srch-panel">

        <% if params[:browse] %>
          <li><h1><i class="fa fa-folder-open-o"></i><%= t("browse.title")%></h1></li>
        <% elsif params[:catalogue] %>
          <li><h1><i class="fa fa-tags"></i><%=t('catalogue.title')%></h1></li>
        <% elsif params[:directory] %>
          <li><h1><i class="fa fa-tags"></i><%=t('catalogue.title_directory')%></h1></li>
        <% elsif params[:archive] %>
          <li><h1><i class="fa fa-tags"></i><%=t('catalogue.title_archive')%></h1></li>
        <% else %>
          <li><h1><i class="fa fa-search"></i><%= t("search.title")%></h1></li>
        <% end %>

        <div filter_type="category_ids" archive_filter="true" directory_filter="true" catalogue_filter="true" style="display:none" class="filter_set">
          <button href="#" status="showMore" class="showMoreButton btn-openclose opened_button">
            <a>-</a>
          </button>
          <span class="filter_title"><%=t("search.titles.filter_by")%> <span class="filter_title_bold"><%=t("search.titles.subject")%></span></span>
          <hr class="separator">
          <ul class="opened_filter">
            <% Vish::Application.config.catalogue["categories"].each do |categoryId| %>
              <li class="filter_item" filter_key="category_ids" categoryId="<%=categoryId%>" filter="<%=categoryId%>"><%=t('catalogue.categories.' + categoryId)%></li>
            <% end %>
          </ul>
        </div>

        <div filter_type="super_type" search_filter="true" style="display:none" class="filter_set">
          <button href="#" status="showMore" class="showMoreButton btn-openclose opened_button">
            <a>-</a>
          </button>
          <span class="filter_title"><%=t("search.titles.filter_by")%> <span class="filter_title_bold"><%=t("search.titles.type")%></span></span>
          <hr class="separator">
          <ul class="opened_filter">
            <li class="filter_item" filter_key="type" filter="User" exclusive><%=t("search.models.user")%></li>
            <li class="filter_item" filter_key="type" filter="Learning_object" exclusive><%=t("search.models.learning_object")%></li>
          </ul>
        </div>

        <div filter_type="lo_type" opens_with="Learning_object" style="display:none" class="filter_set">
          <button href="#" status="showMore" class="showMoreButton btn-openclose opened_button">
            <a>-</a>
          </button>
          <span class="filter_title"><%=t("search.titles.filter_by")%> <span class="filter_title_bold"><%=t("search.titles.learning_object")%></span></span>
          <hr class="separator">
          <ul class="opened_filter">
            <% VishConfig.getSearchModels(:include_users => false).each do |model| %>
              <li class="filter_item" filter_key="type" filter="<%=model%>"><%=t("search.models.#{model.downcase}")%></li>
            <% end %>
          </ul>
        </div>

        <div filter_type="resouce_type" id="resource_type" directory_filter="true" opens_with="Resource" style="display:none" class="filter_set">
            <button href="#" status="showMore" class="showMoreButton btn-openclose opened_button">
              <a>-</a>
            </button>
            <span class="filter_title"><%=t("search.titles.filter_by")%> <span class="filter_title_bold"><%=t("search.titles.resource")%></span></span>
            <hr class="separator">
            <ul class="opened_filter">
              <% VishConfig.getAvailableNotMainResourceModels(:include_subtypes=>true).reject!{|e| e=="Document" }.each do |res| %>
                <li class="filter_item" filter_key="type" filter="<%=res%>"><%=t("search.models.#{res.downcase}")%></li>
              <%end%>
            </ul>
        </div>

        <div filter_type="language" style="display:none" search_filter="true" archive_filter="true" directory_filter="true" catalogue_filter="true" class="filter_set">
          <button href="#" status="showMore" class="showMoreButton btn-openclose opened_button">
            <a>-</a>
          </button>
          <span class="filter_title"><%=t("search.titles.filter_by")%> <span class="filter_title_bold"><%=t("search.titles.lang")%></span></span>
          <hr class="separator">
          <ul class="opened_filter">
            <% search_resource_languages.each do |lang| %>
              <li class="filter_item" filter_key="language" filter="<%=lang[1]%>" exclusive><%=lang[0]%></li>
            <% end %>
          </ul>
        </div>

        <div filter_type="age_type" opens_with="Learning_object" archive_filter="true" directory_filter="true" catalogue_filter="true" style="display:none" class="filter_set">
          <button href="#" status="showMore" class="showMoreButton btn-openclose opened_button">
            <a>-</a>
          </button>
          <span class="filter_title"><%=t("search.titles.filter_by")%> <span class="filter_title_bold"><%=t("search.titles.age")%></span></span>
          <hr class="separator">
          <ul class="opened_filter">
            <li class="filter_item" filter_key="age" filter="range1" exclusive><%=t("search.filters.age_range_1")%></li>
            <li class="filter_item" filter_key="age" filter="range2" exclusive><%=t("search.filters.age_range_2")%></li>
            <li class="filter_item" filter_key="age" filter="range3" exclusive><%=t("search.filters.age_range_3")%></li>
          </ul>
        </div>

        <div filter_type="tags" search_filter="true" archive_filter="true" directory_filter="true" catalogue_filter="true" style="display:none" class="filter_set">
          <button href="#" status="showMore" class="showMoreButton btn-openclose opened_button">
            <a>-</a>
          </button>
          <span class="filter_title"><%=t("search.titles.filter_by")%> <span class="filter_title_bold"><%=t("search.titles.tag")%></span></span>
          <hr class="separator">
          <ul class="opened_filter ul_selected_tags" id="selected_tags_ul"></ul>
          <ul class="opened_filter ul_tags" id="tags_ul"></ul>
        </div>

        </div>
      </div>

    <div class="filter-results">

      <div class="search_filters filters-applied">

        <%= render partial: 'help/help_search' %>

        <% if searchTerms %>
          <div><%= t("search.terms")%>: <span class="n_results"><%=params[:q]%></span></div>
        <% end %>
        <div><span class="n_results" id="n_results"><%=@search_result.total_entries%></span> <%= t("search.results")%></div>
        <div class="title_applied_filters"><%= t("search.applied_filters")%></div>

          <div id="applied_filters" class="filter_boxes"></div>
          <div class="clear"></div>

          <div class="dropdown dropdown_order_by" id="order_by_selector_search">

            <button class="btn btn-secundary dropdown-toggle" type="button" data-toggle="dropdown">
              <span>
                <%if params[:browse] %>
                  <i class="fa fa-tachometer"></i>
                  <%=t('order_by.popularity')%>
                <%elsif params[:catalogue] || params[:directory] %>
                  <% if (params[:catalogue] && VishConfig.getCatalogueModels() === ["Excursion"]) || (params[:directory] && VishConfig.getDirectoryModels() === ["Excursion"]) %>
                    <i class="fa fa-tachometer"></i>
                    <%=t('order_by.quality')%>
                  <% else %>
                    <i class="fa fa-tachometer"></i>
                    <%=t('order_by.popularity')%>
                  <% end %>
                <% else %>
                  <i class="fa fa-sort-amount-desc"></i>
                  <%=t('order_by.relevance')%>
                <%end%>
              </span><i class="fa fa-angle-down"></i>
            </button>

            <ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="order_by_selector_search">
              <li role="presentation">
                <a href="#" sort-by-key="relevance">
                  <i class="fa fa-sort-amount-desc"></i>
                  <span><%=t('order_by.relevance')%> </span>
                </a>
              </li>
             <li role="presentation">
                <a href="#" sort-by-key="popularity">
                  <i class="fa fa-tachometer"></i>
                  <span><%=t('order_by.popularity')%> </span>
                </a>
              </li>

              <li role="presentation">
                <a href="#"  sort-by-key="created_at">
                  <i class="fa fa-bolt"></i>
                  <span><%=t('order_by.created_at')%> </span>
                </a>
              </li>
              <li role="presentation" class="only_for_excursion disabled" title="<%=t('search.titles.tooltip2')%>">
                <a href="#" sort-by-key="quality">
                  <i class="fa fa-check-circle"></i>
                  <span><%=t('order_by.quality')%> </span>
                </a>
              </li>
              <li role="presentation" class="disable_for_user" title="">
                <a href="#" sort-by-key="updated_at">
                  <i class="fa fa-pencil"></i>
                  <span><%=t('order_by.updated_at')%> </span>
                </a>
              </li>
                <li role="presentation" class="disable_for_user" title="">
                  <a href="#" sort-by-key="visits">
                    <i class="fa fa-eye"></i>
                    <span><%=t('order_by.visits')%> </span>
                  </a>
                </li>
                <li role="presentation" class="disable_for_user" title="">
                  <a href="#" sort-by-key="favorites">
                    <i class="fa fa-star"></i>
                    <span><%=t('order_by.favorites')%> </span>
                  </a>
                </li>
            </ul>
          </div>
          <div class="clear"></div>

        </div>
        <div id="search-all" class="searched-items">
          <ul class="items">
            <% @search_result.each do |r| %>
                <% unless r.nil? %>
                  <%= render partial: "#{ r.class.to_s.tableize }/search_result",
                             locals:  { r.class.to_s.underscore.to_sym => r, :prefix_id => "search" }  %>
                <% end %>
            <% end %>
          </ul>
        </div>
        <div class="loader_pagination">
          <div class="pageless-loader" style="text-align:center;width:100%;">
            <div class="msg" style="font-size:2em">
              <%=t('search.loading')%>
            </div>
            <%= render :partial => 'layouts/loading'%>
          </div>
        </div>
        <div id="last_content_shown" style="display:none"> <%=t('search.end')%></div>
      </div>

    </div>
  </div>

</section>


<%= content_for :javascript do %>

  Vish.Search.init({
    "object_types": <%=raw VishConfig.getSearchModels(:include_users => false)%>,
    "resource_types": <%=raw VishConfig.getAvailableNotMainResourceModels(:include_subtypes=>true).reject!{|e| e=="Document" }%>,
    "result_resource_types": "<%= (searchTerms ? extract_types(@search_result) : "") %>",
    "num_pages": <%=@search_result.num_pages%>,
    "url": window.location.href,
    "tags": "<%=extract_tags(@search_result)%>",
    "sort_by_disable_tooltip": "<%=t('search.titles.tooltip')%>",
    "only_for_excursion_tooltip": "<%=t('search.titles.tooltip2')%>",
    "excursionsInCatalogue": <%= VishConfig.getCatalogueModels().include? "Excursion" %>,
    "excursionsInDirectory": <%= VishConfig.getDirectoryModels().include? "Excursion" %>,
    "excursionsInArchive": <%= VishConfig.getArchiveModels().include? "Excursion" %>
  });

  <% if searchTerms %>
    $(".search-query").val("<%=params[:q]%>");
  <% end %>

  SocialStream.Repository.show();

  $("footer#footer").addClass("fixed-bottom");

<% end %>
